{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# The Theta Model\n",
    "\n",
    "The Theta model of Assimakopoulos & Nikolopoulos (2000) is a simple method for forecasting the involves fitting two $\\theta$-lines, forecasting the lines using a Simple Exponential Smoother, and then combining the forecasts from the two lines to produce the final forecast.  The model is implemented in steps:\n",
    "\n",
    "\n",
    "1. Test for seasonality\n",
    "2. Deseasonalize if seasonality detected\n",
    "3. Estimate $\\alpha$ by fitting a SES model to the data and $b_0$ by OLS.\n",
    "4. Forecast the series\n",
    "5. Reseasonalize if the data was deseasonalized.\n",
    "\n",
    "The seasonality test examines the ACF at the seasonal lag $m$.  If this lag is significantly different from zero then the data is deseasonalize using `statsmodels.tsa.seasonal_decompose` use either a multiplicative method (default) or additive. \n",
    "\n",
    "The parameters of the model are $b_0$ and $\\alpha$ where $b_0$ is estimated from the OLS regression\n",
    "\n",
    "$$\n",
    "X_t = a_0 + b_0 (t-1) + \\epsilon_t\n",
    "$$\n",
    "\n",
    "and $\\alpha$ is the SES smoothing parameter in\n",
    "\n",
    "$$\n",
    "\\tilde{X}_t = (1-\\alpha) X_t + \\alpha \\tilde{X}_{t-1}\n",
    "$$\n",
    "\n",
    "The forecasts are then \n",
    "\n",
    "$$\n",
    " \\hat{X}_{T+h|T} = \\frac{\\theta-1}{\\theta} \\hat{b}_0\n",
    "                     \\left[h - 1 + \\frac{1}{\\hat{\\alpha}}\n",
    "                     - \\frac{(1-\\hat{\\alpha})^T}{\\hat{\\alpha}} \\right]\n",
    "                     + \\tilde{X}_{T+h|T}\n",
    "$$\n",
    "\n",
    "Ultimately $\\theta$ only plays a role in determining how much the trend is damped.  If $\\theta$ is very large, then the forecast of the model is identical to that from an Integrated Moving Average with a drift,\n",
    "\n",
    "$$\n",
    "X_t = X_{t-1} + b_0 + (\\alpha-1)\\epsilon_{t-1} + \\epsilon_t.\n",
    "$$\n",
    "\n",
    "Finally, the forecasts are reseasonalized if needed.\n",
    "\n",
    "This module is based on:\n",
    "\n",
    "* Assimakopoulos, V., & Nikolopoulos, K. (2000). The theta model: a decomposition\n",
    "  approach to forecasting. International journal of forecasting, 16(4), 521-530.\n",
    "* Hyndman, R. J., & Billah, B. (2003). Unmasking the Theta method.\n",
    "  International Journal of Forecasting, 19(2), 287-290.\n",
    "* Fioruci, J. A., Pellegrini, T. R., Louzada, F., & Petropoulos, F.\n",
    "  (2015). The optimized theta method. arXiv preprint arXiv:1503.03529."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Imports\n",
    "\n",
    "We start with the standard set of imports and some tweaks to the default matplotlib style."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import pandas_datareader as pdr\n",
    "import seaborn as sns\n",
    "\n",
    "plt.rc(\"figure\", figsize=(16, 8))\n",
    "plt.rc(\"font\", size=15)\n",
    "plt.rc(\"lines\", linewidth=3)\n",
    "sns.set_style(\"darkgrid\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Load some Data\n",
    "\n",
    "We will first look at housing starts using US data. This series is clearly seasonal but does not have a clear trend during the same. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "reader = pdr.fred.FredReader([\"HOUST\"], start=\"1980-01-01\", end=\"2020-04-01\")\n",
    "data = reader.read()\n",
    "housing = data.HOUST\n",
    "housing.index.freq = housing.index.inferred_freq\n",
    "ax = housing.plot()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We fit specify the model without any options and fit it. The summary shows that the data was deseasonalized using the multiplicative method.  The drift is modest and negative, and the smoothing parameter is fairly low. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from statsmodels.tsa.forecasting.theta import ThetaModel\n",
    "\n",
    "tm = ThetaModel(housing)\n",
    "res = tm.fit()\n",
    "print(res.summary())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The model is first and foremost a forecasting method.  Forecasts are produced using the `forecast` method from fitted model. Below we produce a hedgehog plot by forecasting 2-years ahead every 2 years. \n",
    "\n",
    "**Note**: the default $\\theta$ is 2."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "forecasts = {\"housing\": housing}\n",
    "for year in range(1995, 2020, 2):\n",
    "    sub = housing[: str(year)]\n",
    "    res = ThetaModel(sub).fit()\n",
    "    fcast = res.forecast(24)\n",
    "    forecasts[str(year)] = fcast\n",
    "forecasts = pd.DataFrame(forecasts)\n",
    "ax = forecasts[\"1995\":].plot(legend=False)\n",
    "children = ax.get_children()\n",
    "children[0].set_linewidth(4)\n",
    "children[0].set_alpha(0.3)\n",
    "children[0].set_color(\"#000000\")\n",
    "ax.set_title(\"Housing Starts\")\n",
    "plt.tight_layout(pad=1.0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We could alternatively fit the log of the data.  Here it makes more sense to force the deseasonalizing to use the additive method, if needed.  We also fit the model parameters using MLE.  This method fits the IMA\n",
    "\n",
    "$$ X_t = X_{t-1} + \\gamma\\epsilon_{t-1} + \\epsilon_t $$\n",
    "\n",
    "where $\\hat{\\alpha}$ = $\\min(\\hat{\\gamma}+1, 0.9998)$ using `statsmodels.tsa.SARIMAX`. The parameters are similar although the drift is closer to zero."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "tm = ThetaModel(np.log(housing), method=\"additive\")\n",
    "res = tm.fit(use_mle=True)\n",
    "print(res.summary())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The forecast only depends on the forecast trend component,\n",
    "$$\n",
    "\\hat{b}_0\n",
    "                     \\left[h - 1 + \\frac{1}{\\hat{\\alpha}}\n",
    "                     - \\frac{(1-\\hat{\\alpha})^T}{\\hat{\\alpha}} \\right],\n",
    "$$\n",
    "\n",
    "the forecast from the SES (which does not change with the horizon), and the seasonal. These three components are available using the `forecast_components`. This allows forecasts to be constructed using multiple choices of $\\theta$ using the weight expression above. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "res.forecast_components(12)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Personal Consumption Expenditure\n",
    "\n",
    "We next look at personal consumption expenditure. This series has a clear seasonal component and a drift. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "reader = pdr.fred.FredReader([\"NA000349Q\"], start=\"1980-01-01\", end=\"2020-04-01\")\n",
    "pce = reader.read()\n",
    "pce.columns = [\"PCE\"]\n",
    "pce.index.freq = \"QS-OCT\"\n",
    "_ = pce.plot()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Since this series is always positive, we model the $\\ln$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "mod = ThetaModel(np.log(pce))\n",
    "res = mod.fit()\n",
    "print(res.summary())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Next we explore differenced in the forecast as $\\theta$ changes. When $\\theta$ is close to 1, the drift is nearly absent.  As $\\theta$ increases, the drift becomes more obvious."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "forecasts = pd.DataFrame(\n",
    "    {\n",
    "        \"ln PCE\": np.log(pce.PCE),\n",
    "        \"theta=1.2\": res.forecast(12, theta=1.2),\n",
    "        \"theta=2\": res.forecast(12),\n",
    "        \"theta=3\": res.forecast(12, theta=3),\n",
    "        \"No damping\": res.forecast(12, theta=np.inf),\n",
    "    }\n",
    ")\n",
    "_ = forecasts.tail(36).plot()\n",
    "plt.title(\"Forecasts of ln PCE\")\n",
    "plt.tight_layout(pad=1.0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Finally, `plot_predict` can be used to visualize the predictions and prediction intervals which are constructed assuming the IMA is true."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ax = res.plot_predict(24, theta=2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We conclude be producing a hedgehog plot using 2-year non-overlapping samples."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ln_pce = np.log(pce.PCE)\n",
    "forecasts = {\"ln PCE\": ln_pce}\n",
    "for year in range(1995, 2020, 3):\n",
    "    sub = ln_pce[: str(year)]\n",
    "    res = ThetaModel(sub).fit()\n",
    "    fcast = res.forecast(12)\n",
    "    forecasts[str(year)] = fcast\n",
    "forecasts = pd.DataFrame(forecasts)\n",
    "ax = forecasts[\"1995\":].plot(legend=False)\n",
    "children = ax.get_children()\n",
    "children[0].set_linewidth(4)\n",
    "children[0].set_alpha(0.3)\n",
    "children[0].set_color(\"#000000\")\n",
    "ax.set_title(\"ln PCE\")\n",
    "plt.tight_layout(pad=1.0)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
